package com.project.movie.controller.note;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.movie.common.enums.BusinessType;
import com.project.movie.common.enums.ResultCode;
import com.project.movie.domain.MovieItem;
import com.project.movie.domain.MovieNote;
import com.project.movie.domain.MoviePerformer;
import com.project.movie.domain.Result;
import com.project.movie.service.MovieItemService;
import com.project.movie.service.MovieNoteService;
import com.project.movie.service.MoviePerformerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 电影资讯controller
 * @date 2025/02/06 11:49
 */
@Controller
@ResponseBody
@RequestMapping("note")
public class MovieNoteController {

    @Autowired
    private MovieNoteService movieNoteService;
    @Autowired
    private MovieItemService movieItemService;
    @Autowired
    private MoviePerformerService moviePerformerService;

    /** 分页获取电影资讯 */
    @PostMapping("getMovieNotePage")
    public Result getMovieNotePage(@RequestBody MovieNote movieNote) {
        Page<MovieNote> page = new Page<>(movieNote.getPageNumber(),movieNote.getPageSize());
        QueryWrapper<MovieNote> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(movieNote.getTitle()),MovieNote::getTitle,movieNote.getTitle())
                .like(StringUtils.isNotBlank(movieNote.getIntroduce()),MovieNote::getIntroduce,movieNote.getIntroduce())
                .like(StringUtils.isNotBlank(movieNote.getContent()),MovieNote::getContent,movieNote.getContent())
                .eq(StringUtils.isNotBlank(movieNote.getItemId()),MovieNote::getItemId,movieNote.getItemId())
                .orderByDesc(MovieNote::getCreateTime);
        Page<MovieNote> movieNotePage = movieNoteService.page(page, queryWrapper);
        return Result.success(movieNotePage);
    }

    @GetMapping("getMovieNoteIndex")
    public Result getMovieNoteIndex() {
        QueryWrapper<MovieNote> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(MovieNote::getCreateTime).last("limit 8");
        List<MovieNote> noteList = movieNoteService.list(queryWrapper);
        return Result.success(noteList);
    }

    /** 根据id获取电影资讯 */
    @GetMapping("getMovieNoteById")
    public Result getMovieNoteById(@RequestParam("id")String id) {
        MovieNote movieNote = movieNoteService.getById(id);
        if (StringUtils.isNotBlank(movieNote.getItemId())) {
            MovieItem movieItem = movieItemService.getById(movieNote.getItemId());
            movieNote.setMovieItem(movieItem);
            List<String> performer = new ArrayList<>();
            QueryWrapper<MoviePerformer> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(MoviePerformer::getItemId,id);
            List<MoviePerformer> performerList = moviePerformerService.list(queryWrapper);
            for (MoviePerformer moviePerformer : performerList) {
                performer.add(moviePerformer.getName());
            }
            movieItem.setPerformer(StringUtils.join(performer,","));
        }
        return Result.success(movieNote);
    }

    /** 保存电影资讯 */
    @PostMapping("saveMovieNote")
    public Result saveMovieNote(@RequestBody MovieNote movieNote) {
        boolean save = movieNoteService.save(movieNote);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑电影资讯 */
    @PostMapping("editMovieNote")
    public Result editMovieNote(@RequestBody MovieNote movieNote) {
        boolean save = movieNoteService.updateById(movieNote);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除电影资讯 */
    @GetMapping("removeMovieNote")
    public Result removeMovieNote(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                movieNoteService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("电影资讯id不能为空！");
        }
    }

}
